/**
 * 
 */
package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Textbox;

import com.google.gdata.data.contacts.ContactEntry;
import com.lt.flashcard.model.service.ImportGoogleContactService;
import com.lt.flashcard.model.service.Repository;

/**
 * @author Administrator
 *
 */
public class GmailContactsCtrl extends SelectorComposer<Component> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Wire
	private Textbox gmail;
	@Wire
	private Include incContactList;
	@Wire
	private Textbox password;
	
	private List<ContactEntry> contacts;

	/* (non-Javadoc)
	 * @see org.zkoss.zk.ui.select.SelectorComposer#doBeforeCompose(org.zkoss.zk.ui.Page, org.zkoss.zk.ui.Component, org.zkoss.zk.ui.metainfo.ComponentInfo)
	 */
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		// TODO Auto-generated method stub
		contacts = null;
		return super.doBeforeCompose(page, parent, compInfo);
	}

	/**
	 * @return the gmail
	 */
	public Textbox getGmail() {
		return gmail;
	}

	/**
	 * @param gmail the gmail to set
	 */
	public void setGmail(Textbox gmail) {
		this.gmail = gmail;
	}
	
	/**
	 * @return the contacts
	 */
	public List<ContactEntry> getContacts() {
		return contacts;
	}

	/**
	 * @param contacts the contacts to set
	 */
	public void setContacts(List<ContactEntry> contacts) {
		this.contacts = contacts;
	}

	/**
	 * @return the incContactList
	 */
	public Include getIncContactList() {
		return incContactList;
	}

	/**
	 * @param incContactList the incContactList to set
	 */
	public void setIncContactList(Include incContactList) {
		this.incContactList = incContactList;
	}
	
	/**
	 * @return the password
	 */
	public Textbox getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(Textbox password) {
		this.password = password;
	}

	@Listen("onClick=#btnLoadContacts")
	public void onLoadContacts() {
		ImportGoogleContactService srv = Repository.getInstance().getGmailContactService();
		contacts = srv.getContactsFromGoogleAcc(gmail.getValue(), password.getValue());
		if(contacts != null) {
			incContactList.invalidate();
			incContactList.setVisible(true);
		}
	}
}